using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using JGSY.CMS.LowCode.Platform.Domain.Interfaces;

namespace JGSY.CMS.LowCode.Platform.Domain.Entities
{
    /// <summary>
    /// 优惠券实体
    /// 用于管理系统的优惠券和促销代码
    /// </summary>
    [Table("coupons")]
    public class Coupon : ITenantEntity
    {
        /// <summary>
        /// 主键ID
        /// </summary>
        [Key]
        public long Id { get; set; }

        /// <summary>
        /// 优惠券代码
        /// </summary>
        [Required]
        [StringLength(50)]
        public string Code { get; set; } = string.Empty;

        /// <summary>
        /// 优惠券名称
        /// </summary>
        [Required]
        [StringLength(255)]
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 优惠券描述
        /// </summary>
        [Column(TypeName = "text")]
        public string? Description { get; set; }

        /// <summary>
        /// 优惠类型
        /// percent=百分比折扣, fixed=固定金额折扣, free_shipping=免费配送
        /// </summary>
        [Required]
        [StringLength(20)]
        public string Type { get; set; } = string.Empty;

        /// <summary>
        /// 优惠值
        /// 根据类型不同含义不同：百分比时为折扣百分比，固定金额时为折扣金额
        /// </summary>
        [Column(TypeName = "decimal(10,2)")]
        public decimal Value { get; set; }

        /// <summary>
        /// 最小订单金额
        /// 使用优惠券的最低消费要求
        /// </summary>
        [Column(TypeName = "decimal(10,2)")]
        public decimal? MinOrderAmount { get; set; }

        /// <summary>
        /// 最大折扣金额
        /// 百分比折扣时的封顶金额
        /// </summary>
        [Column(TypeName = "decimal(10,2)")]
        public decimal? MaxDiscountAmount { get; set; }

        /// <summary>
        /// 使用次数限制
        /// 0表示无限制
        /// </summary>
        public int UsageLimit { get; set; } = 0;

        /// <summary>
        /// 已使用次数
        /// </summary>
        public int UsedCount { get; set; } = 0;

        /// <summary>
        /// 每用户使用次数限制
        /// 0表示无限制
        /// </summary>
        public int UsageLimitPerUser { get; set; } = 1;

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 状态（启用/禁用）
        /// 0=禁用, 1=启用
        /// </summary>
        public byte Status { get; set; } = 1;

        /// <summary>
        /// 创建人
        /// </summary>
        [Required]
        public long CreatedBy { get; set; }

        /// <summary>
        /// 修改人
        /// </summary>
        public long? UpdatedBy { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 创建人导航属性
        /// </summary>
        [ForeignKey(nameof(CreatedBy))]
        public User? Creator { get; set; }

        /// <summary>
        /// 修改人导航属性
        /// </summary>
        [ForeignKey(nameof(UpdatedBy))]
        public User? Updater { get; set; }

        /// <summary>
        /// 租户标识符
        /// </summary>
        public string TenantId { get; set; } = string.Empty;
    }
}
